package cn.enilu.flash.api.controller.park;

import cn.enilu.flash.bean.entity.park.ParkingCarRef;
import cn.enilu.flash.bean.entity.park.ParkingContract;
import cn.enilu.flash.bean.entity.park.ParkingInfo;
import cn.enilu.flash.bean.vo.query.SearchFilter;
import cn.enilu.flash.conf.properties.LocalFileProperties;
import cn.enilu.flash.enums.body.QuoteUnit;
import cn.enilu.flash.enums.body.YesOrNo;
import cn.enilu.flash.service.park.ParkingCarRefService;
import cn.enilu.flash.service.park.ParkingContractService;

import cn.enilu.flash.bean.core.BussinessLog;
import cn.enilu.flash.bean.constant.factory.PageFactory;
import cn.enilu.flash.bean.dictmap.CommonDict;
import cn.enilu.flash.bean.enumeration.BizExceptionEnum;
import cn.enilu.flash.bean.exception.GunsException;
import cn.enilu.flash.bean.vo.front.Rets;

import cn.enilu.flash.service.park.ParkingInfoService;
import cn.enilu.flash.utils.BeanUtil;
import cn.enilu.flash.utils.DateUtil;
import cn.enilu.flash.utils.Maps;
import cn.enilu.flash.utils.ToolUtil;
import cn.enilu.flash.utils.factory.Page;

import cn.enilu.flash.warpper.ParkInfoWarpper;
import cn.enilu.flash.warpper.ParkingContractWarpper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/parkcontract")
public class ParkingContractController {
	private  Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private LocalFileProperties localFileProperties;
	@Autowired
	private ParkingContractService parkingContractService;
	@Autowired
	private ParkingCarRefService parkingCarRefService;
	@Autowired
	private ParkingInfoService parkingInfoService;

	@RequestMapping(value = "/list",method = RequestMethod.GET)
	public Object list(@RequestParam(required = false) String communityId,
					   @RequestParam(required = false) String quoteType,
					   @RequestParam(required = false) String yesOrNo) {
		Page<ParkingContract> page = new PageFactory<ParkingContract>().defaultPage();
		page.addFilter("communityId", SearchFilter.Operator.EQ, communityId);
		page.addFilter("quoteType", SearchFilter.Operator.EQ, quoteType);
		if(StringUtils.isNotBlank(yesOrNo)){
			if(YesOrNo.YES.getCode() == Integer.parseInt(yesOrNo)){
				page.addFilter("startDate", SearchFilter.Operator.LT, DateUtil.format(new Date()));
				page.addFilter("endDate", SearchFilter.Operator.GT, DateUtil.format(new Date()));
			}else{
				page.addFilter("endDate", SearchFilter.Operator.LT, DateUtil.format(new Date()));
			}
		}
		page.setSort(new Sort(Sort.Direction.DESC, "parkingId", "signDate"));
		page = parkingContractService.queryPage(page);
		List list = (List) new ParkingContractWarpper(BeanUtil.objectsToMaps(page.getRecords()), localFileProperties).warp();
		page.setRecords(list);
		return Rets.success(page);
	}
	@RequestMapping(method = RequestMethod.POST)
	@BussinessLog(value = "编辑车位合同", key = "name",dict= CommonDict.class)
	public Object save(@ModelAttribute ParkingContract parkingContract){
		int range = parkingContract.getQuoteRange();
		int unit = parkingContract.getQuoteUnit();
		Date start = parkingContract.getStartDate();
		Date end = null;
		if(unit == QuoteUnit.YEAR.getCode()){
			end = DateUtil.getAfterYearDate(start, range);
		}else if(unit == QuoteUnit.QUARTER.getCode()){
			end = DateUtil.getAfterMonthDate(start, range * 3);
		}else if(unit == QuoteUnit.MONTH.getCode()){
			end = DateUtil.getAfterMonthDate(start, range);
		}
		parkingContract.setEndDate(end);
		if(parkingContract.getId()==null){
			ParkingInfo info = parkingInfoService.get(parkingContract.getParkingId());
			info.setOwnerFlag(parkingContract.getQuoteType());

			ParkingCarRef ref = new ParkingCarRef();
			ref.setCarId(0L);
			ref.setCommunityId(parkingContract.getCommunityId());
			ref.setCustId(parkingContract.getCustId());
			ref.setParkingId(parkingContract.getParkingId());
			ref.setBuildingId(0L);
			ref.setRoomId(0L);
			ref.setStartDate(parkingContract.getStartDate());
			ref.setEndDate(parkingContract.getEndDate());
			ref.setState(DateUtil.compareDate(new Date(), parkingContract.getEndDate()) ? 0 : 1);
			parkingContractService.insert(parkingContract);
			parkingCarRefService.insert(ref);
			parkingInfoService.update(info);
		}
		return Rets.success();
	}
	@RequestMapping(method = RequestMethod.DELETE)
	@BussinessLog(value = "删除车位合同", key = "id",dict= CommonDict.class)
	public Object remove(Long id){
		if (ToolUtil.isEmpty(id)) {
			throw new GunsException(BizExceptionEnum.REQUEST_NULL);
		}
		parkingContractService.delete(id);
		return Rets.success();
	}
}